<?php

require_once('../../db_pozr.inc');
require_once('lib/nusoap.php');

function SendRawMeasurement($measurements)
{
	$conn = open_db();
	$result = false;
	
	foreach($measurements as $measurement)
	{
		if($measurement["m_type"])
			$mt = "TRUE";
		else
			$mt = "FALSE";
				
		$query = sprintf("INSERT INTO pozr_raw_measurements
						  (hash, signal_type, measurement_type, latitude, longitude, altitude, signal_strength,
						   time_recorded)
						  VALUES ('%s', '%s', %s, %f, %f, %f, %d, NOW())",
            	 $measurement["hash"], $measurement["s_type"], $mt, $measurement["lat"], $measurement["lon"],
            	 $measurement["alt"], $measurement["strength"]);
	    mysql_query($query);
	    
		// Check result
		// This shows the actual query sent to MySQL, and the error. Useful for debugging.
		if (!$result) {
		  $message  = 'Invalid query: ' . mysql_error() . "\n";
		  $message .= 'Whole query: ' . $query;
		  die($message);
		}

		if ((mysql_errno($conn) == 0) && (mysql_affected_rows($conn) > 0))
			$result = true;
	}
	
	close_db($conn);
	return $result;
}


function PrintRawMeasurements()
{
	$conn = open_db();
	$query = sprintf("SELECT * FROM pozr_raw_measurements");
    $result = mysql_query($query);
    
	// Check result
	// This shows the actual query sent to MySQL, and the error. Useful for debugging.
	if (!$result) {
	  $message  = 'Invalid query: ' . mysql_error() . "\n";
	  $message .= 'Whole query: ' . $query;
	  die($message);
	}

	while($row = mysql_fetch_assoc($result))
	{
		print_r($row);
	}
	
	close_db($conn);
}

?>